package server;

import network.Message;
import network.SocketConnection;

import java.io.IOException;
import java.net.Socket;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

public class MultiClientsEmulation {
    public static AtomicInteger count = new AtomicInteger(0);
    public static Object countMonitor = new Object();

    private static class Client {
        private SocketConnection socketConnection;

        private Client(SocketConnection socketConnection) {
            this.socketConnection = socketConnection;
        }

        public void chatWithServer(String message) {
            try {
                Message msg = new Message("person2", "Admin", "/snd", message, new Date());
                msg.setRoom("default");
                socketConnection.write(msg);
                /*Message in = (Message) socketConnection.read();
                synchronized (countMonitor) {
                    System.out.println(String.valueOf(++count) + " " + in.getMessage());
                }*/
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void read() throws IOException, ClassNotFoundException {
            System.out.println(String.valueOf(count.incrementAndGet()) + " " + ((Message) socketConnection.read()).getMessage());
        }

        public void registrate(String message) {
            try {
                Message msg = new Message(Thread.currentThread().getName(), "Admin", "/chid", message, new Date());
                socketConnection.write(msg);
                System.out.println(((Message) socketConnection.read()).getMessage());
                Message msg2 = new Message(message, "Admin", "/pwd", message, new Date());
                socketConnection.write(msg2);
                System.out.println(((Message) socketConnection.read()).getMessage());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        public void history() {
            try {
                Message msg = new Message(Thread.currentThread().getName(), "Admin", "/hist", "", new Date());
                socketConnection.write(msg);
                while (true) {
                    System.out.println(((Message) socketConnection.read()).getMessage());
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        public void changeRoom(String room) {
            try {
                Message message = new Message(Thread.currentThread().getName(), "Admin", "/chroom", room, new Date());
                socketConnection.write(message);
                while (true) {
                    System.out.println(((Message) socketConnection.read()).getMessage());
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            try {
                Client client = new Client(new SocketConnection(new Socket("localhost", 888)));
                new Thread(() -> {
                    client.registrate(Thread.currentThread().getName());
                    new Thread(() -> {
                        while (true) {
                            try {
                                client.read();
                            } catch (ClassNotFoundException | IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                    for (int j = 0; j < 100; j++) {
                        client.chatWithServer(String.valueOf(j));
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    //client.history();
                }).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
